package com.example.javamaday.leetcode.sort;

public class LcSort0912_02 {
    public static void main(String[] args) {
        new LcSort0912_02().sortArray(LcSort0912_01.nums);
    }

    // 插入排序
    public int[] sortArray(int[] nums) {
        if (nums.length <= 1) {
            return nums;
        }
        for (int i = 0; i < nums.length; i++) {
            // 往前插入，直到位置合适了
            for (int j = i; j > 0; j--) {
                if (nums[j - 1] > nums[j]) {
                    exchange(nums, j - 1, j);
                } else {
                    break;
                }
            }
        }
        return nums;
    }

    public void exchange(int[] nums, int a, int b) {
        int temp = nums[a];
        nums[a] = nums[b];
        nums[b] = temp;
    }
}
